package com.hitqz.robot.biz.flow.component.patrol;

import com.alibaba.fastjson.JSONObject;
import com.hitqz.robot.api.business.model.dto.FlowContextDto;
import com.hitqz.robot.api.business.model.dto.FlowPatrolPreAndEndTicketsDto;
import com.hitqz.robot.api.common.dto.robot.RobotGoTargetPosDto;
import com.hitqz.robot.api.business.model.dto.FlowPatrolTicketsDto;
import com.hitqz.robot.api.common.entity.RobotMapPos;
import com.hitqz.robot.api.common.entity.TicketsTask;
import com.hitqz.robot.biz.business.dispatch.DispatchService;
import com.hitqz.robot.biz.flow.util.LiteFlowUtil;
import com.hitqz.robot.biz.service.RobotMapPosService;
import com.hitqz.robot.biz.service.RobotService;
import com.hitqz.robot.driver.model.IRobotPosDto;
import com.hitqz.robot.driver.parent.RobotDriverService;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Slf4j
@LiteflowComponent(value = "GoTargetDevPosition", name = "[巡检]前往设备点")
@AllArgsConstructor
public class GoTargetDevPosition extends NodeComponent {

    private final RobotDriverService robotDriverService;

    private final RobotService robotService;

    private final RobotMapPosService robotMapPosService;

    private final DispatchService dispatchService;


    @Override
    public void process() throws Exception {
        FlowContextDto dto =  this.getRequestData();
        TicketsTask task = dto.getTicketsTask();
        log.info("任务数据 {}", task.getParseOrderParams());
        FlowPatrolTicketsDto params = JSONObject.parseObject(task.getParseOrderParams(), FlowPatrolTicketsDto.class);
        log.info("{} ---params: {}", task.getParseOrderParams(), JSONObject.toJSONString(params));
        FlowPatrolPreAndEndTicketsDto endParams = JSONObject.parseObject(task.getParseOrderParams(), FlowPatrolPreAndEndTicketsDto.class);
        if (params.getPosId()==null && endParams.getEndPosId()==null){
            log.warn("没有站点信息,不执行");
            return;
        }
        Integer posId = params.getPosId() != null ? params.getPosId() : endParams.getEndPosId();
        RobotMapPos robotMapPos = robotMapPosService.getById(posId);
        IRobotPosDto iRobotPosDto = robotDriverService.getWebPos();
        if (Objects.equals(iRobotPosDto.getCurrentStation(), robotMapPos.getCode()) && iRobotPosDto.isReached()) {
            log.info("已在设备点{},直接执行动作",robotMapPos.getCode());
        } else {
            log.info("前往设备点{}", robotMapPos.getCode());
//            robotDriverService.robotTaskGoTarget(
//                    RobotGoTargetPosDto.builder()
//                            .robot(robotService.getDefaultRobot())
//                            .code(robotMapPos.getCode())
//                            .charge(false).posId(robotMapPos.getId().intValue())
//                            .taskId(UUID.randomUUID().toString().replace("-","")).build());
            //调用规划路径算法的前往站点
            dispatchService.go2TargetPosition(robotMapPos, false);
            TimeUnit.SECONDS.sleep(1);
            iRobotPosDto = robotDriverService.getWebPos();
            while ( !(Objects.equals(iRobotPosDto.getCurrentStation(), robotMapPos.getCode()) && iRobotPosDto.isReached())) {
                iRobotPosDto = robotDriverService.getWebPos();
                TimeUnit.SECONDS.sleep(1);
                //外部中断标志
                LiteFlowUtil.checkInterruptFlag();
            }
            log.info("起始点机器人到达位置了");
        }
        log.info("到达点位坐标 {}---- x: {}, y: {}, yaw {}", iRobotPosDto.getCurrentStation(),
                iRobotPosDto.getX(), iRobotPosDto.getY(), iRobotPosDto.getAngle());
    }
}